

cc.Class({
    extends: cc.Component,

    properties: {
        //玩家节点.
        playerNode:{
            default: null,
            type: cc.Node
        },
        coinManagerNode:{
            default: null,
            type: cc.Node
        },
        //所有的预制体板子类型.
        boardPrefabs: {
            default: [],
            type: cc.Prefab,
            visible: true
        },
        //板子间隙.
        boardGap: 500,
        //倍数递减.
        scaleMinus: 0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.createPositionY = -460;
        //当前场上生成的板子数量.
        this.currentFloorCount = 0;
        this.coinManager = this.coinManagerNode.getComponent('CoinManager');
        //对象池数组.
        this.boardPools = new Array(this.boardPrefabs.length);
        //初始化对象池.
        for (let i = 0; i < this.boardPools.length; ++i) {
            this.boardPools[i] = new cc.NodePool();
            for (let j = 0; j < 5; ++j){
                this.boardPools[i].put(cc.instantiate(this.boardPrefabs[i]));
            }
        }
        this.currentScale = 1;
    },

    start () {
        this.createBoard(10);
    },

    // update (dt) {},

    isPlayerUp(){
        return this.playerNode.getComponent('Player').isVelocityPositive();
    },

    //生成板子(并且在范围内生成金币)).
    createBoard(num){
        var min = this.createPositionY;
        for (let i = 0, l = num; i < l; i++) {
            var id = this.getBoardPrefabNum();
            if(this.currentFloorCount == 0 && Global.isFirstIn){
                //新手教程第一块板子为中心.
                id = 0;
            }
            //var clone = cc.instantiate(this.boardPrefabs[id]);
            var clone = this.getBoard(id);
            clone.scaleX = this.currentScale;
            //var clone = this.getBoard(id);
            clone.name = id.toString();
            clone.parent = this.node;
            var x = cc.randomMinus1To1() * 250;
            var y = this.createPositionY;
            clone.position = cc.v2(x,y);
            this.createPositionY += this.boardGap;
            //设置板子层数.
            this.currentFloorCount++;
            clone.getComponent('Board').setFloor(this.currentFloorCount);
            clone.getComponent('Board').init();
            //新手教程第一块板子不动.
            if(this.currentFloorCount == 1 && Global.isFirstIn){
                //固定位置在中心.
                clone.position = cc.v2(0,y);
                //先写固定位置.
                this.coinManager.createCoin(4.4,-326.3);
                //不要放到对象池.
                clone.getComponent(cc.Collider).tag = 9;
            }
        }
        var max = this.createPositionY;
        this.minusScale();
        //生成金币.
        this.coinManager.createCoins(min,max);
    },

    //获取一个随机的板子类型的ID.
    //现在的概率都一样.
    getBoardPrefabNum(){
        //取随机数.
        var randomNum = cc.random0To1()*this.boardPrefabs.length;
        //取得的整数即类型ID号.
        var pickNum = Math.floor(randomNum);
        return pickNum;
    },

    //判断降落后是否需要生成.
    judgeCreateState(playerFloor){
        //是否在五层以内.
        if(playerFloor >= this.currentFloorCount - 5){
            this.createBoard(10);
        }
    },

    //根据Id获取一块实体板子.
    getBoard(boardId){
        let clone = null;
        if (this.boardPools[boardId].size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
            clone = this.boardPools[boardId].get();
        } else { // 如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 cc.instantiate 重新创建
            clone = cc.instantiate(this.boardPrefabs[boardId]);
        }
        return clone;
    },

    //把实例化出的板子放入对象池.
    putBoard(boardNode){
        //根据名字选择对象池放入.
        var id = parseInt(boardNode.name);
        this.boardPools[id].put(boardNode);
    },
    
    //生成比例缩减.
    minusScale(){
        if(this.currentScale <= 0.5) return;
        this.currentScale -= this.scaleMinus;
    },
});
